/*
 * Copyright (c) 2016-2019 fafa.com.cn. All Rights Reserved.
 */


/* 依赖
    <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.2.50</version>
     </dependency>
     <dependency>
     <groupId>org.jdom</groupId>
     <artifactId>jdom2</artifactId>
     <version>2.0.6</version>
     </dependency>
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.*;

/**
 * @author Roninwz
 * @date 2019/3/1 下午14:15
 * @since 1.0.0
 */

import com.alibaba.fastjson.JSONObject;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

public class XMLTOJSON {


    public static void main(String[] args) {
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><string xmlns=\"http://tways.vicp.net/\">{\"ResultCode\":\"0\",\"ResultValue\":[{\"errMsg\":{\"List\":[{\"cardCode\":\"WX82746887\",\"vipName\":\"宁江波\",\"mobile\":\"13572161442\",\"birthday\":\"2018-12-19\",\"genderName\":\"女\",\"address\":\"\",\"cardStatusName\":\"正常\",\"rankName\":\"TGP普通卡\",\"branchCode\":\"001020\",\"branchName\":\"西北分公司\",\"currPoint\":\"955\",\"currAmt\":\"0\",\"giftQty\":\"3\"}]}}]}</string>";
        String xml2 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><string xmlns=\"http://tways.vicp.net/\"><text>{\"ResultCode\":\"0\",\"ResultValue\":[{\"errMsg\":{\"List\":[{\"cardCode\":\"WX82746887\",\"vipName\":\"宁江波\",\"mobile\":\"13572161442\",\"birthday\":\"2018-12-19\",\"genderName\":\"女\",\"address\":\"\",\"cardStatusName\":\"正常\",\"rankName\":\"TGP普通卡\",\"branchCode\":\"001020\",\"branchName\":\"西北分公司\",\"currPoint\":\"955\",\"currAmt\":\"0\",\"giftQty\":\"3\"}]}}]}</text></string>";
        String xml3 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><string xmlns=\"http://tways.vicp.net/\"><text>{\"ResultCode\":\"0\",\"ResultValue\":[{\"errMsg\":{\"List\":[{\"cardCode\":\"WX82746887\",\"vipName\":\"宁江波\",\"mobile\":\"13572161442\",\"birthday\":\"2018-12-19\",\"genderName\":\"女\",\"address\":\"\",\"cardStatusName\":\"正常\",\"rankName\":\"TGP普通卡\",\"branchCode\":\"001020\",\"branchName\":\"西北分公司\",\"currPoint\":\"955\",\"currAmt\":\"0\",\"giftQty\":\"3\"}]}}]}</text><div><span>1234444444</span></div></string>";

        try {
            JSONObject json = xml2JSON(xml3.getBytes());
            System.out.println(json.toJSONString());
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static JSONObject xml2JSON(byte[] xml) throws JDOMException, IOException {
        JSONObject json = new JSONObject();
        InputStream is = new ByteArrayInputStream(xml);
        SAXBuilder sb = new SAXBuilder();
        org.jdom2.Document doc = sb.build(is);
        Element root = doc.getRootElement();
//        System.out.println("root:"+root.getText().getClass());
//        System.out.println("root:"+root.getContent());
//        System.out.println("root:"+root.getName());
//        System.out.println("root:"+root.getChildren().size());
        if (root.getChildren().size() > 0) {
            json.put(root.getName(), iterateElement(root));
        } else if (root.getChildren().size() == 0) {
            json.put(root.getName(), root.getTextTrim());
        }
        return json;
    }

    private static JSONObject iterateElement(Element element) {
        List node = element.getChildren();
        System.out.println("node:" + node);
        Element et = null;
        JSONObject obj = new JSONObject();
        List list = null;
        for (int i = 0; i < node.size(); i++) {
            list = new LinkedList();
            et = (Element) node.get(i);
            if (et.getTextTrim().equals("")) {
                if (et.getChildren().size() == 0)
                    continue;
                if (obj.containsKey(et.getName())) {
                    list = (List) obj.get(et.getName());
                }
                list.add(iterateElement(et));
                obj.put(et.getName(), list);
            } else {
                if (obj.containsKey(et.getName())) {
                    list = (List) obj.get(et.getName());
                }
                list.add(et.getTextTrim());
                obj.put(et.getName(), list);
            }
        }
        return obj;
    }

}
